<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    const a = [1,2,3,4,5,6]
    Array.prototype.filter = function (fn) {
        const result = []
        for (let i = 0; i < this.length; i++) {
            if( fn(this[i],i,this) )result.push(this[i])
        }
        return result
    }
    const arr =  a.filter(e=>{
        return e>2
    })
    Array.prototype.reduce = function (fn,total) {
        if(this.length === 0)return
        let i = 0
        if( !total ){
            i = 1
            total = this[0]
        }
        for ( i ; i < this.length; i++) {
            total = fn(total,this[i],i,this)
        }
        return total
    }
    const num = [1,2,3].reduce(function (total,num) {
        return total + num
    },0)

    /*
    * 由此可见，要将一个类数组对象转换为一个真正的数组，必须具备以下条件：
　　  1、该类数组对象必须具有length属性，用于指定数组的长度。如果没有length属性，那么转换后的数组是一个空数组。
　　  2、该类数组对象的属性名必须为数值型或字符串型的数字
　　  ps: 该类数组对象的属性名可以加引号，也可以不加引号
    * */
    Array.from = function (arrayLike,fn,that) {

        if(!arrayLike.length && !(arrayLike instanceof Set)){ // 如果没有length属性直接结束
            return []
        }
        let len = arrayLike.length || arrayLike.size;
        const flag = arrayLike instanceof Set
        if(flag){
            arrayLike = [...arrayLike]
        }
        let result = []
        for (let i = 0; i < len; i++) {
            if( flag ){
                result.push(arrayLike[i])
                // result.push(setIter.next().value[1])
            }else{
                result.push(arrayLike[i])
            }
        }
        if(fn){
            return result.map(function (value, index, array) {
                if(that){
                    return  fn.call(that,value,index,array)
                }else{
                    return  fn(value,index,array)
                }
            })
        }
        return result
    }

    /*console.log( Array.from({
        0: 'aa',
        1: 'bb',
        2: 'cc',
        length: 3
    })  );*/
    /*let arrayLike = {
        'name': 'tom',
        'age': '65',
        'sex': '男',
        'friends': ['jane','john','Mary'],
        length: 4
    }
    console.log(Array.from(arrayLike,(v,i)=>{
        return i
    }));*/
    let arr1 = [12,45,97,9797,564,134,45642]
    let set = new Set(arr1)

    console.log(Array.from(set,item=>item+1))


</script>
</body>
</html>
